package com.khtn.dao.impl;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.TypedQuery;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Join;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import javax.persistence.metamodel.EntityType;
import javax.persistence.metamodel.Metamodel;

import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import com.khtn.dao.TourDao;
import com.khtn.model.Tour;

@Repository
@Transactional(readOnly = true)
public class TourDaoImpl implements TourDao {

	private EntityManager entityManager = null;

	@PersistenceContext
	public void setEntityManager(EntityManager entityManager) {
		this.entityManager = entityManager;
	}

	public Tour create(Tour tour) {
		// TODO Auto-generated method stub
		return null;
	}

	public Tour read(int id) {
		// TODO Auto-generated method stub
		return entityManager.find(Tour.class, id);
	}

	public Tour update(Tour tour) {
		// TODO Auto-generated method stub
		return null;
	}

	public void delete(Tour tour) {
		// TODO Auto-generated method stub
		
	}
	//Get 10 new tour
	@SuppressWarnings("unchecked")
	public Collection<Tour> findTenNewTour(){
//		CriteriaBuilder cb = entityManager.getCriteriaBuilder();
//		CriteriaQuery<Tour> cq=cb.createQuery(Tour.class);
//		Root<Tour> tour=cq.from(Tour.class);
//		cq.select(tour);
//		cq.orderBy(cb.asc(tour.<Integer>get("tour_id")));
//		List<Tour> listTour= new ArrayList<Tour>();
//		TypedQuery<Tour> query=entityManager.createQuery(cq);
//		try{
//			listTour=query.setMaxResults(10).getResultList();
//		}
//		catch(Exception ex){
//			System.out.print("Can read list tour");
//		}

		return 	entityManager.createQuery("select t from Tour t order by t.tourId").setMaxResults(10).getResultList();
	}

}
